import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import vuetify from './plugins/vuetify'
import i18n from './plugins/i18n'
import { useAuthStore } from './stores/auth'
import { useSocketStore } from './stores/socket'
import * as XLSX from 'xlsx'

// 将XLSX库挂载到全局window对象
window.XLSX = XLSX

// WebSocket功能已集成在socketStore中，无需额外导入

const app = createApp(App)
const pinia = createPinia()

app.use(pinia)
app.use(router)
app.use(vuetify)
app.use(i18n)

// 初始化认证状态
const authStore = useAuthStore()
authStore.init()

// 初始化socket store
const socketStore = useSocketStore()

// 监听路由变化，处理WebSocket连接
router.beforeEach((to, from, next) => {
  // 如果用户已登录，初始化WebSocket连接
  if (authStore.isAuthenticated && !to.meta.noSocket) {
    // 初始化socket store
    socketStore.init()
  }
  next()
})

// 在应用关闭时清理WebSocket连接
window.addEventListener('beforeunload', () => {
  socketStore.cleanup()
})

app.mount('#app')
